package  src
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author ...
	 */
	public class Player extends MovieClip
	{
		var cima : Boolean;
		var baixo : Boolean;
		var esquerda : Boolean;
		var direita : Boolean;
		var enter : Boolean;
		
		var velocidade : Number;
		
		var px : Number;
		var py : Number;
		
		var dentroCarro : Boolean;
		
		public function Player() 
		{
			velocidade = 2;
			dentroCarro = false;
			
			addEventListener(Event.ADDED_TO_STAGE, iniciarStage);
		}
		
		public function iniciarStage(e:Event):void
		{
			Main._instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, pressionada);
			Main._instance.stage.addEventListener(KeyboardEvent.KEY_UP, despressionada);
		}
		
		public function pressionada(e:KeyboardEvent) : void
		{
			if (e.keyCode == Keyboard.UP)
			{
				cima = true;
			}
			if (e.keyCode == Keyboard.DOWN)
			{
				baixo = true;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				esquerda = true;
			}
			if (e.keyCode == Keyboard.RIGHT)
			{
				direita = true;
			}
			if (e.keyCode == Keyboard.E)
			{
				enter = true;
			}
		}
		
		public function despressionada(e:KeyboardEvent) : void
		{
			if (e.keyCode == Keyboard.UP)
			{
				cima = false;
			}
			if (e.keyCode == Keyboard.DOWN)
			{
				baixo = false;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				esquerda = false;
			}
			if (e.keyCode == Keyboard.RIGHT)
			{
				direita = false;
			}
			if (e.keyCode == Keyboard.E)
			{
				Main._instance.game.level.numClick = 1;
				enter = false;
			}
		}
		
		public function update():void
		{
			
			
			entrarCarro();
			
			trace(dentroCarro);
			
			if (cima)
			{
				px += Math.sin(this.rotation * (Math.PI / 180)) * velocidade;
				py -= Math.cos(this.rotation * (Math.PI / 180)) * velocidade;
			}
			
			if (baixo)
			{
				px -= Math.sin(this.rotation * (Math.PI / 180));
				py += Math.cos(this.rotation * (Math.PI / 180));
			}
			
			if (direita)
			{
				this.rotation += 5;
			}
			
			if (esquerda)
			{
				this.rotation -= 5;
			}
			
			if (dentroCarro)
			{
				//this.visible = false;
			}
			
			else
			{
				//this.visible = true;
			}
			
		}
		
		public function add() : void 
		{
			
			Main._instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, pressionada);
			Main._instance.stage.addEventListener(KeyboardEvent.KEY_UP, despressionada);
			
		}
		
		public function remove() : void 
		{
			
			cima = false;
			baixo = false;
			esquerda = false;
			direita = false;
			
			Main._instance.stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressionada);
			Main._instance.stage.removeEventListener(KeyboardEvent.KEY_UP, despressionada);
			
		}
		
		public function entrarCarro():void
		{
			if (this.hitTestObject(Main._instance.game.level.carro) && enter && !dentroCarro)
			{
				dentroCarro = true;
				enter = false;
			}
			if (dentroCarro && enter)
			{
				dentroCarro = false;
				enter = false;
			}
		}
	}

}